#pragma once
#include "filter.h"
#include <QString>
#include <QFile>
#include <QTextStream>
#include "Converter.h"

template<class T>
class CSVOutput :
	public Filter
{
public:
	CSVOutput(int propertiesCount, const QString &filename) : Filter(propertiesCount, 0), propertiesCount(propertiesCount), filename(filename)
	{
		file = new QFile(filename);
		file->open(QIODevice::WriteOnly);

		textStream = new QTextStream(file);
		textStream->setLocale(QLocale::system());
	}

	~CSVOutput(void)
	{
		textStream->flush();
		file->close();

		delete textStream;
		delete file;
	}

	virtual void Process()
	{
		for (int i = 0; i < propertiesCount; ++i)
		{

			for (int j = 0; j < inputs[i]->rows; ++j)
			{
				T *p = inputs[i]->ptr<T>(j);
				for (int k = 0; k < inputs[i]->cols; ++k)
				{
					(*textStream) << p[k];
					(*textStream) << ";";
				}
			}

		}

		(*textStream) << "\n";
	}

	virtual void ProcessInvalid()
	{
		(*textStream) << "Invalid\n";
	}

private:
	int propertiesCount;
	QString filename;
	QFile *file;
	QTextStream *textStream;
};

